import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import { getBannerList } from '../../utils/api'
const bannerSlice = createSlice({
    name: 'banner',
    initialState: {
        bannerList: [] as BannerRowType[]
    },
    reducers: {
        // 初始化bannerList
        // action={type:"",payload:[]}
        bannerListInit(state, action) {
            state.bannerList = action.payload
        }
    }
})
const { bannerListInit } = bannerSlice.actions
// bus_id:请求接口需要的参数, 商户id
export const getBannerListThunk = createAsyncThunk('getBannerListThunk', async (bus_id: string | undefined, { dispatch }) => {
    // 发送数据请求
    const { data } = await getBannerList(bus_id)
    // 将请求结果使用dispatch方法派发给bannerListInit
    if (data.code === 200) {
        dispatch(bannerListInit(data.data))
    }
})
const bannerReducer = bannerSlice.reducer
export default bannerReducer